#ifndef _TEXTONGROUP_H
#define _TEXTONGROUP_H

#include "Texton.h"
#include "MultiScalePyramid.h"
#include <vector>
#include <algorithm>

using namespace std;

// Represents a group of related textons 
class TextonGroup : public Texton
{

public:

	TextonGroup()
	{
		m_vecTextons.clear();
		m_surroundingHist = NULL;
	}

	void AddTexton(Texton* texton);
	void TextonGroup::UpdateImage(IplImage* orig);
	void Save(const string& strPath);

	/**
	 * Returns true if one of the textons in the group intersects with given texton, false otherwise
	 */
	bool Intersects(const Texton* texton)
	{
		for(vector<Texton*>::const_iterator itr = m_vecTextons.begin(); itr != m_vecTextons.end(); itr++ ) {
			if ((*itr)->Intersects(texton)) {
				return true;
			}
		}
		return false;
	}

	int Size() 
	{
		return (int)(m_vecTextons.size());
	}

	/**
	 * Mark all textons in the group on the image using specified color
	 */
	void MarkTextons(IplImage* inputImage, CvScalar color);

	// Holds all textons in the group
	vector<Texton*> m_vecTextons;

private:
	IplImage* m_mask;
	CvMat* m_surroundingHist;
	IplImage* m_surroundings;
public:
	IplImage* GetMask() { return m_mask;}
	void SetMask(IplImage* m) { m_mask = cvCreateImage(cvGetSize(m),8,1); cvCopy(m,m_mask);}
	void SetSurroundingHist(CvMat* v) { m_surroundingHist = v;}
	CvMat* GetSurroundingHist() { return m_surroundingHist;}
	IplImage* GetSurroundings() { return m_surroundings;}
	void SetSourroundings(IplImage* i) { m_surroundings = cvCreateImage(cvGetSize(i),8,i->nChannels); cvCopy(i,m_surroundings);}
	~TextonGroup() { 
		cvReleaseImage(&m_mask); 
		if(m_surroundingHist) { cvReleaseMat(&m_surroundingHist); } 
		if(m_surroundings) {cvReleaseImage(&m_surroundings);}
	}
};

#endif